package com.hf.eagle.web.struts.admin.manager;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebiz.ssi.util.EncryptUtils;
import com.ebiz.ssi.web.struts.bean.UploadFile;
import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Keys;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * @author Xu,XiaoYuan
 * @version 2011.9.07 21:23
 */
public class CompanyDescAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.edit(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;

		Pager pager = (Pager) dynaBean.get("pager");
		String mod_id = (String) dynaBean.get("mod_id");

		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setMod_id(Long.valueOf(mod_id));
		newsInfo.setIs_del(0);
		newsInfo.setInfo_state(1);

		Long recordCount = super.getFacade().getNewsInfoService().getNewsInfoCount(newsInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		newsInfo.getRow().setFirst(pager.getFirstRow());
		newsInfo.getRow().setCount(pager.getLastRow());

		List<NewsInfo> list = super.getFacade().getNewsInfoService().getNewsInfoPaginatedList(newsInfo);
		request.setAttribute("entityList", list);

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");

		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setMod_id(Long.valueOf(mod_id));
		newsInfo.setIs_del(0);

		newsInfo = super.getFacade().getNewsInfoService().getNewsInfo(newsInfo);
		if (null != newsInfo) {
			Accessory acce = new Accessory();
			acce.setFile_level(SysConstant.MAIN_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", newsInfo.getId());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);

			newsInfo.setAccessory(acce);
		}

		super.copyProperties(form, newsInfo);

		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(SysConstant.NEWS_INFO_STATE);
		sd.setIs_del(0);
		List<SysDictionary> infoStateList = super.getFacade().getSysDictionaryService().getSysDictionaryList(sd);
		request.setAttribute("infoStateList", infoStateList);

		return mapping.findForward("input");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String acce_id = (String) dynaBean.get("acce_id");
		String description = (String) dynaBean.get("description");

		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute(Keys.ADMIN_USER);

		NewsInfo newsInfo = new NewsInfo();
		super.copyProperties(newsInfo, form);
		newsInfo.setAdd_user_id(userInfo.getUser_id());
		newsInfo.setIs_del(0);

		List<Accessory> accessoryList = new ArrayList<Accessory>();

		List<UploadFile> uploadFileList = this.uploadFile(form, "", true, SysConstant.NEWS_INFO_MAIN_IMG_SIZE);
		if (null != uploadFileList && uploadFileList.size() > 0) {
			String ctx = getServlet().getServletContext().getRealPath(File.separator);
			int width = SysConstant.NEWS_INFO_VICE_IMG_SIZE[0];
			int height = SysConstant.NEWS_INFO_VICE_IMG_SIZE[1];

			for (UploadFile uploadFile : uploadFileList) {
				if (uploadFile.getFileSize() > 0) {
					String savePath = uploadFile.getFileSavePath();

					Accessory acc_main = new Accessory();
					acc_main.setFile_type(SysConstant.IMAGE);
					acc_main.setFile_size(String.valueOf(uploadFile.getFileSize()));
					acc_main.setFile_name(uploadFile.getFileName());
					acc_main.setSave_url(uploadFile.getFileSavePath());
					acc_main.setFile_level(SysConstant.MAIN_IMG);// 主图
					acc_main.setDescription(description);
					acc_main.setIs_del(0);

					accessoryList.add(acc_main);

					String file_original_path = ctx + StringUtils.substringBeforeLast(savePath, ".") + "_original."
							+ StringUtils.substringAfterLast(savePath, ".");
					String file_desc_path = ctx + StringUtils.substringBeforeLast(savePath, ".") + "_small."
							+ StringUtils.substringAfterLast(savePath, ".");

					String acc_vice_url = this.resizeImageByFixed(file_original_path, file_desc_path, width, height);// 生成小图片

					String file_save_name = StringUtils.substringAfterLast(acc_vice_url, "/");

					Accessory acc_vice = new Accessory();
					acc_vice.setFile_type(SysConstant.IMAGE);
					acc_vice.setFile_size(width + "*" + height);
					acc_vice.setFile_name(StringUtils.substringBeforeLast(uploadFile.getFileName(), ".") + "_small."
							+ uploadFile.getExtension());
					acc_vice.setSave_url(StringUtils.substringBeforeLast(savePath, "/") + "/" + file_save_name);
					acc_vice.setFile_level(SysConstant.SUB_IMG);// 副图
					acc_vice.setDescription(description);
					acc_vice.setIs_del(0);

					accessoryList.add(acc_vice);
				}
			}
		}

		newsInfo.setAccessoryList(accessoryList);
		newsInfo.getMap().put("acce_id", acce_id);
		newsInfo.getMap().put("description", description);

		if (null == newsInfo.getId()) {
			super.getFacade().getNewsInfoService().createNewsInfo(newsInfo);
		} else {
			newsInfo.setEdit_datetime(new Date());
			newsInfo.setEdit_user_id(userInfo.getUser_id());
			super.getFacade().getNewsInfoService().modifyNewsInfo(newsInfo);
		}

		return this.edit(mapping, form, request, response);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("input");
	}

	public static void main(String[] args) {
		System.out.println(EncryptUtils.MD5Encode("1"));
	}
}
